package de.tudarmstadt.ukp.wikisniffer.userstudy.datastore;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import de.tudarmstadt.ukp.wikisniffer.userstudy.datastore.ResultStore.Task;
import de.tudarmstadt.ukp.wikisniffer.userstudy.util.Question;
import de.tudarmstadt.ukp.wikisniffer.userstudy.util.QuestionGroup;

public class QuestionStore {
	
	private File questionFile;
	
	private QuestionGroup currentQuestionGroup;
	
	private List<QuestionGroup> questionGroups;
	
	public void init(String filePath) throws IOException{
		questionFile = new File(filePath);
		
		questionGroups = new ArrayList<QuestionGroup>();
		BufferedReader reader = new BufferedReader(new FileReader(questionFile));
		String line;
		QuestionGroup questionGroup = null;
		while((line = reader.readLine()) != null){
			if(!line.contains("\t")) {
				questionGroup = new QuestionGroup(line);
				questionGroups.add(questionGroup);
			}
			else {
				questionGroup.add(new Question(line));
			}
		}
	}
	
	public QuestionGroup getCurrentQuestionGroup(){
		return currentQuestionGroup;
	}
	
	public QuestionGroup getNewQuestionGroup(){
		
		if(questionGroups.size() == 0){
			System.err.println("No questions left!");
			currentQuestionGroup = null;
			return currentQuestionGroup;
		}
		
		Random random = new Random();
		
		int randomInt = random.nextInt(questionGroups.size());
		currentQuestionGroup = questionGroups.get(randomInt);
		questionGroups.remove(randomInt);
		
		return currentQuestionGroup;
	}

	public boolean hasQuestionGroup(){
		return questionGroups.size() != 0;
	}

	public int getHalfAmountOfGroups() {
		return questionGroups.size() / 2;
	}

	
}
